﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using ComicIsland.Common;
using ComicIsland.Common.Entities;

/**
 * 创建时间：2014/12/24 11:19:46
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.DownloadConverter
{
    /// <summary>
    /// 已下载的章节标识
    /// </summary>
    public class ComicChapterDownloadedFlagConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
             //new SolidColorBrush(Helper.HexToColor(System.Convert.ToString(Application.Current.Resources["DefaultBackgroundBrush"])));            
            SolidColorBrush result = new SolidColorBrush(Colors.Transparent);
            try
            {
                string comicAndPartId = string.Empty;
                if (value != null)
                {
                    comicAndPartId = value.ToString();
                }

                if (!string.IsNullOrWhiteSpace(comicAndPartId))
                {
                    var cpIds = comicAndPartId.Split('#');
                    if (cpIds != null && cpIds.Length == 2)
                    {
                        int comicId = 0, partId = 0;
                        int.TryParse(cpIds[0], out comicId);
                        int.TryParse(cpIds[1], out partId);

                        var item = App.comicDbHelper.ChapterGetOne(comicId, partId);
                        if (item != null && DownloadComicChapterStatus.Completed.ToString().Equals(item.DownloadStatus))
                        {
                            result = new SolidColorBrush(Helper.HexToColor(System.Convert.ToString(Application.Current.Resources["DefaultForegroundBrush"])));
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }
}
